<template>
  <div class="page-container">
    <div class="content" v-if="cityData">
      <h1>{{ cityData.title }}</h1>
      <div class="image-wrapper">
        <img :src="cityData.image" :alt="cityData.name" />
      </div>
      <div class="description">
        <h2>{{ cityData.about.title }}</h2>
        <p v-for="(paragraph, index) in cityData.about.paragraphs" :key="index">
          {{ paragraph }}
        </p>
        <h3>{{ cityData.attractions.title }}</h3>
        <ul>
          <li v-for="(attraction, index) in cityData.attractions.list" :key="index">
            {{ attraction.name }} - {{ attraction.description }}
          </li>
        </ul>
      </div>
    </div>
    <div class="content" v-else>
      <h1>城市信息未找到</h1>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { useRoute } from 'vue-router'
import data from '../db/data.json'

const route = useRoute()
const cityData = ref<any>(null)

// 根据路由参数获取城市数据
const loadCityData = () => {
  const cityId = route.params.id as string
  const city = data.cities.find(c => c.id === cityId)
  cityData.value = city || null
}

// 组件挂载时加载数据
onMounted(() => {
  loadCityData()
})

// 监听路由变化，重新加载数据
watch(() => route.params.id, () => {
  loadCityData()
})
</script>
